<?php
namespace Admin\Controller;
use Think\Controller;
class PtController extends Controller {
	public function listpt() {
		header('Content-Type:text/html; charset=utf-8');
		$MP2P = M('mp2p');
		$list = $MP2P->select();
		$this->assign('menu_name', '平台管理');
		$this->assign('list', $list);
		$this->display();
	}
	
	public function update() {
		header('Content-Type:text/html; charset=utf-8');
		$id = (int)I('post.ptid');
		$this->assign('ptid', $id);
		if ($id == 0) {
			$this->assign('memo', '添加网贷平台');
			$this->display();
		} else {
			$MP2P = M('mp2p');
			$pt = $MP2P->where('id='.$id)->find();
			if ($pt == false) {
				echo "查询出错";
			} else {
				$this->assign('memo', '编辑网贷平台');
				$this->assign('pt', $pt);
				$this->display();
			}
		}
	}
	
	public function del() {
		$tmp = explode(',', I('post.ptid'));
		$PT = M('mp2p');
		while (list($key, $val) = each($tmp)) {
			$ret = $PT->where('id='.$val)->delete(); // 删除PT数据
			if ($ret == false) {
				$data['status'] = 0;
				$data['content'] = '删除SQL错误:'.$val;
				$this->ajaxReturn($data);
			}
		}
		$data['status'] = 1;
		$data['content'] = '删除成功';
		$this->ajaxReturn($data);
	}
	
	public function do_update() {
		$ptid = (int)I('post.pt_id');
		$name = I('post.pt_name');
		$addr = I('post.pt_addr');
		$url = I('post.pt_url');
		$level = I('post.pt_level');
		$status = I('post.pt_status');
		
		$str='ptid='.$ptid.' name='.$name.' addr='.$addr.' url='.$url.' level='.$level.' status='.$status;
		if ($name == "" || $addr == "" || $url == "" || $level == "" || $status == "") {
			$data['status'] = 0;
			$data['content'] = '参数非法:'.$str;
		} else {
			$PT = M('mp2p');
			$nemo = "添加";
			if ($ptid != 0) { 
				$record['id'] = $ptid;
				$nemo = "修改";
			}
			$record['name'] = $name;
			$record['addr'] = $addr;
			$record['url'] = $url;
			$record['level'] = (int)$level;
			$record['logo'] = "aaa";
			$record['is_active'] = (int)$status;
			$record['desc'] = "aaa";
			if ($PT->create($record) != true) {	
				$data['status'] = 0;
				$data['content'] = $nemo.'失败';
			} else {
				if ($ptid != 0) {
					$PT->save();
				} else {
					$PT->add();
				}
				$data['status'] = 1;
				$data['content'] = $nemo.'成功';
			}
		}
		$this->ajaxReturn($data);
	}
}
?>